package demo.service;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import demo.dao.UserDao;
import demo.data.UserDO;
import demo.data.UserDTO;

@Service
public class UserService{
	
	@Autowired
    private UserDao userDao;

   
    public UserDO getById(Long id) {
        return userDao.getById(id);
    }
    
    public void insert(UserDO user) {
        userDao.insert(user);
    }
    
    public void update(UserDO user) {
        userDao.update(user);
    }
    
    public void delete(Long id) {
        userDao.delete(id);
    }
    
    public List<UserDO> getAllUsers() {
        return userDao.getAllUsers();
    }
    
    public List<UserDTO> getAllUserDTOs() {
        return userDao.getAllUserDTOs();
    }
    
    
    // @Transactional(rollbackForClassName={"java.lang.Exception"})
    @Transactional
    public void addUsersTrans(Map<String,Object> map)throws Exception{    	
    	addUsers(map);    	
    }
    
    public void addUsersNoTrans(Map<String,Object> map)throws Exception{
    	addUsers(map);
    }
    
    private void addUsers(Map<String,Object> map)throws Exception{
    	UserDO user1 = new UserDO();
    	long id = 3;
    	user1.setId(id);
    	user1.setName("name-"+id);
    	userDao.insert(user1);
    	
    	UserDO user2 = new UserDO();
    	id = 4;
    	user2.setId(id);
    	user2.setName("name-"+id);
    	userDao.insert(user2);
    }
    
}